package com.way.capture.utils;

import android.os.FileObserver;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

public class ScreenshotFileObserver extends FileObserver {
    private static final String TAG = "ScreenshotFileObserver";
    private OnNewFileChangedListener mListener;

    public ScreenshotFileObserver(String path, OnNewFileChangedListener listener) {
        super(path, FileObserver.CLOSE_WRITE);
        mListener = listener;
    }

    @Override
    public void onEvent(int event, @Nullable String path) {
        switch (event) {
            case FileObserver.CLOSE_WRITE:
                if (TextUtils.isEmpty(path)) {
                    return;
                }
                Log.d(TAG, "onEvent: event = " + CLOSE_WRITE + ", path = " + path);
                if (path.startsWith("Screenshot") || path.startsWith("ScreenRecord")) {
                    mListener.onNewFileChanged(path);
                }
                break;
        }
    }

    public interface OnNewFileChangedListener {
        void onNewFileChanged(String path);
    }
}
